package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamineeExamPaper;
import com.opennetexam.manager.sqlprovider.ExamineeExamPaperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 奶茶
 * Created on 2017/11/14.
 */
@Mapper
public interface ExamineeExamPaperMapper {

    @SelectProvider(type = ExamineeExamPaperProvider.class,method = "getExamineeExamPaperCountBySomeWhere")
    Integer getExamineeExamPaperCountBySomeWhere(@Param(value = "appId") String appId, @Param(value = "examPlanInfoId") String examPlanInfoId, @Param(value = "examSubjectId") String examSubjectId);

    @Select("SELECT examSubjectID,examSceneID,examineeID FROM ExamineeExamPaper WHERE ExamPlanInfoID = #{planId}")
    List<ExamineeExamPaper> findExamineeExamPaperByPlanId(@Param("planId") String planId);

    @Select("SELECT examPaperContent FROM ExamineeExamPaper WHERE ExamPlanInfoID = #{planId} limit 1")
    String findExamPaperToRedis(@Param("planId") String planId);

    @Delete("DELETE FROM ExamineeExamPaper WHERE ExamPlanInfoID = #{planId}")
    void delExamineeExamPaperByPlanId(@Param("planId") String planId);

    @Insert("INSERT INTO ExamineeExamPaper (ExamPaperInfoID,AppID,ExamPlanInfoID,ExamSubjectID,ExamPaperID,ExamSceneID,ExamineeID,PaperVersion,CreateTime,SysState,ExamPaperContent,ExamineeReport) VALUES (#{examPaperInfoID},#{appID},#{examPlanInfoID},#{examSubjectID},#{examPaperID},#{examSceneID},#{examineeID},#{paperVersion},#{createTime},#{sysState},#{examPaperContent},#{examineeReport})")
    Integer insertExamineeExamPaper(ExamineeExamPaper paper);

    @Select("SELECT COUNT(*) FROM ExamineeExamPaper AS x JOIN ( SELECT * FROM ExamSubject WHERE ExamPlanInfoID = #{planId} ) AS y on x.ExamSubjectID = y.ExamSubjectID WHERE x.ExamPlanInfoID = y.ExamPlanInfoID AND x.AppID = y.AppID AND x.ExamPlanInfoID = #{planId} AND x.AppID = #{appId}")
    Integer getCountByPlanIdAndAppId(@Param(value = "planId") String planId,@Param(value = "appId") String appId);

    @Select("SELECT * FROM ExamineeExamPaper WHERE AppID = #{appId} AND  ExamPlanInfoID = #{planId} AND ExamineeID = #{examineeId} AND ExamSceneID = #{sceneId}")
    ExamineeExamPaper findExamineeExamPaperFirstOrDefault(@Param(value = "appId")String appId,@Param(value = "planId") String planId,@Param(value = "examineeId") String examineeId,@Param(value = "sceneId") String sceneId);

    @Update("UPDATE ExamineeExamPaper  \n" +
            "SET AppID = #{appID},ExamPlanInfoID = #{examPlanInfoID},ExamSubjectID = #{examSubjectID},ExamPaperID = #{examPaperID},ExamSceneID = #{examSceneID},ExamineeID = #{examineeID},PaperVersion = #{paperVersion},CreateTime = #{createTime},SysState = #{sysState},ExamPaperContent = #{examPaperContent},ExamineeReport = #{examineeReport} \n" +
            "WHERE ExamPaperInfoID = #{examPaperInfoID}")
    Integer updateExamineeExamPaper(ExamineeExamPaper examineeExamPaper);

    @Select("SELECT * FROM ExamineeExamPaper WHERE AppID = #{appId} AND ExamPlanInfoID = #{planId} AND ExamSubjectID = #{subjectId}")
    List<ExamineeExamPaper> findExamineePaperByAppIdAndPlanIdAndSubjectId(@Param(value = "appId") String appId,@Param(value = "planId") String planId,@Param(value = "subjectId") String subjectId);

    @DeleteProvider(type = ExamineeExamPaperProvider.class,method = "deleteExamineeExamPaper")
    Integer deleteExamineeExamPaper(@Param(value = "examPapers") List<ExamineeExamPaper> examPapers);

    @Select("SELECT * FROM ExamineeExamPaper WHERE ExamineeID = #{examineeId} AND ExamPlanInfoID = #{examPlanInfoId} AND ExamSubjectID = #{examSubjectId} AND AppID = #{appId} LIMIT 1")
    ExamineeExamPaper findExamPaperInfoId(@Param("examineeId") String examineeId, @Param("examPlanInfoId") String examPlanInfoId, @Param("examSubjectId") String examSubjectId, @Param("appId") String appId);

}
